#!/bin/bash
#########################################
#                                       #
# Rotaciona arquivos sem parar as       #
# aplicacoes.                           #
#                                       #
# Fabio Wakim Trentini            -O)   #
# trentas@uol.com.br              /\\   #
# 20011018                       _\_V   #
#                                       #
#########################################
#
# Changelog:
#

CONF=/export/scripts/rotate-nonstop.conf
DATE=`date +%Y%m%d%H%M`

OS=`uname -s`
HOSTNAME=`hostname`
MAILBOX="l-monitor-servers@uolinc.com"

if [ $OS = "Linux" ]; then
  SENDMAIL=/usr/sbin/sendmail
  GZIP=/bin/gzip
elif [ $OS = "SunOS" ]; then
  SENDMAIL=/usr/lib/sendmail
  GZIP=/usr/gnu/bin/gzip
else
  echo "Unknown operating system..."
  exit -1
fi


doit() {

while read line
do
  SOURCE=`echo $line | awk '{ print $1 }'`
  SOURCEFILE=`echo $line | awk '{ print $1 }'| awk -F/ '{ print $NF }'`
  DEST=`echo $line | awk '{ print $2 }'`
  INSTANCE=`echo $line | awk '{ print $3 }'`
  if [ -f $SOURCE ]; then
    if [ -d $DEST ]; then
      if [ $INSTANCE ]; then
        if [ ! -f $DEST/$SOURCEFILE.$INSTANCE.$DATE.gz ]; then
          cat $SOURCE | $GZIP -c > $DEST/$SOURCEFILE.$INSTANCE.$DATE.gz
        else
          echo "Rotated file already exists, aborting"
        fi
      else
        if [ ! -f $DEST/$SOURCEFILE.$DATE.gz ]; then
          cat $SOURCE | $GZIP -c > $DEST/$SOURCEFILE.$DATE.gz
        else
          echo "Rotated file already exists, aborting"
        fi
      fi
      if [ $? = "0" ]; then
        cp /dev/null $SOURCE
      else
        echo "Generic error, aborting"
      fi
    else
      echo "The destination directory was not found, aborting"
    fi
  else
    echo "The source file was not found, aborting"
  fi
done

}

cat $CONF | egrep -v -e '^#' | doit

